using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BONSAI.Common;
using BONSAI.Common.DTO;

namespace BONSAI.DAL
{
	public class ServicesDAO
	{
		public const string SERVICES_GET_BY_ID = "ServicesGetByID";
		public const string SERVICES_GET_ALL = "ServicesGetAll";
		public const string SERVICES_INSERT = "ServicesInsert";
		public const string SERVICES_UPDATE_BY_ID = "ServicesUpdateByID";
		public const string SERVICES_DELETE_BY_ID = "ServicesDeleteByID";

		public const string SERVICESID = "ServicesID";
		public const string NAMEFULL = "NameFull";
		public const string DESCRIPTION = "Description";
		public const string CREATEDATE = "Createdate";

		#region region properties
		public Database Database { get; set; }
		#endregion

		#region Constructor
		public ServicesDAO()
		{
			Database = DatabaseFactory.CreateDatabase(BONSAIConstants.BONSAI_CONNECTION_STRING);
		}
		#endregion


		public ServicesDTO GetByID(string id)
		{
			var command = Database.GetStoredProcCommand(SERVICES_GET_BY_ID);
			try
			{
				Database.AddInParameter(command, SERVICESID, DbType.String, id);
				var dataReader = Database.ExecuteReader(command);
				while (dataReader.Read())
				{
					var item = GetServicesFromDataReader(dataReader);
					return item;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public IList<ServicesDTO> GetAll()
		{
			var command = Database.GetStoredProcCommand(SERVICES_GET_ALL);
			try
			{
				var dataReader = Database.ExecuteReader(command);
				var lstItem = new List<ServicesDTO>();
				while (dataReader.Read())
				{
					var item = GetServicesFromDataReader(dataReader);
					lstItem.Add(item);
				}
				return lstItem;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public string Insert(ServicesDTO services)
		{
			var command = Database.GetStoredProcCommand(SERVICES_INSERT);
			try
			{
				Database.AddOutParameter(command, SERVICESID, DbType.String, 36);
				Database.AddInParameter(command, NAMEFULL, DbType.String, services.NameFull);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, services.Description);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, services.Createdate);
				var dataReader = Database.ExecuteReader(command);
				return Database.GetParameterValue(command, SERVICESID).ToString();
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return null;
		}


		public void UpdateByID(ServicesDTO services)
		{
			var command = Database.GetStoredProcCommand(SERVICES_UPDATE_BY_ID);
			try
			{
				Database.AddInParameter(command, SERVICESID, DbType.String, services.ServicesID);
				Database.AddInParameter(command, NAMEFULL, DbType.String, services.NameFull);
				Database.AddInParameter(command, DESCRIPTION, DbType.String, services.Description);
				Database.AddInParameter(command, CREATEDATE, DbType.DateTime, services.Createdate);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		public void DeleteByID(string servicesid)
		{
			var command = Database.GetStoredProcCommand(SERVICES_DELETE_BY_ID);
			try
			{
				Database.AddInParameter(command, SERVICESID, DbType.String, servicesid);
				var dataReader = Database.ExecuteReader(command);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}


		private static ServicesDTO GetServicesFromDataReader(IDataRecord dataReader)
		{
			var item = new ServicesDTO();
			try
			{

				item.ServicesID = (string.IsNullOrEmpty(Convert.ToString(dataReader[SERVICESID]))) ? "" : Convert.ToString(dataReader[SERVICESID]);
				item.NameFull = (string.IsNullOrEmpty(Convert.ToString(dataReader[NAMEFULL]))) ? "" : Convert.ToString(dataReader[NAMEFULL]);
				item.Description = (string.IsNullOrEmpty(Convert.ToString(dataReader[DESCRIPTION]))) ? "" : Convert.ToString(dataReader[DESCRIPTION]);
				item.Createdate = (string.IsNullOrEmpty(Convert.ToString(dataReader[CREATEDATE]))) ? DateTime.MinValue : DateTime.Parse(dataReader[CREATEDATE].ToString());
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return item;
		}
	}
}

